package com.example.distributedapp.slice;

import com.example.distributedapp.ResourceTable;
import com.example.distributedapp.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice implements IAbilityContinuation {

    private int index = 0;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        Button btnContinue = (Button) findComponentById(ResourceTable.Id_btn_continue);
        btnContinue.setText("迁移(当前数据:" + index + ")");
        btnContinue.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // 获取组网内在线设备列表
                List<String> deviceIds = getAvailableDeviceIds();
                if (deviceIds != null) {
                    // 迁移到组网内设备列表中的第1个设备上
                    continueAbility(deviceIds.get(0));
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public boolean onStartContinuation() {
        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        intentParams.setParam("index" , index);
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        index = (int)intentParams.getParam("index") + 1;
        return true;
    }
    @Override
    public void onCompleteContinuation(int i) {
        terminateAbility();
    }


    /**
     * 获得所有已经连接的所有设备ID
     * @return 设备ID列表
     */
    public static List<String> getAvailableDeviceIds() {
        // 获得DeviceInfo列表，包含了已经连接的所有设备信息
        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(
                DeviceInfo.FLAG_GET_ONLINE_DEVICE);

        // 如果DeviceInfo列表为空则返回
        if (deviceInfoList == null || deviceInfoList.size() == 0) {
            return null;
        }
        // 遍历DeviceInfo列表，获得所有的设备ID
        List<String> deviceIds = new ArrayList<>();
        for (DeviceInfo deviceInfo : deviceInfoList) {
            deviceIds.add(deviceInfo.getDeviceId());
        }
        // 返回所有的设备ID
        return deviceIds;
    }
}
